package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DlTradeClassDO;
import com.bootdo.system.service.DlTradeClassService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-05-31 09:48:03
 */
 
@Controller
@RequestMapping("/system/dlTradeClass")
public class DlTradeClassController {
	@Autowired
	private DlTradeClassService dlTradeClassService;
	
	@GetMapping()
	@RequiresPermissions("system:dlTradeClass:dlTradeClass")
	String DlTradeClass(){
	    return "system/dlTradeClass/dlTradeClass";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dlTradeClass:dlTradeClass")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DlTradeClassDO> dlTradeClassList = dlTradeClassService.list(query);
		int count = dlTradeClassService.count(query);
		PageUtils pageUtils = new PageUtils(dlTradeClassList, count);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dlTradeClass:add")
	String add(){
	    return "system/dlTradeClass/add";
	}

	@GetMapping("/edit/{tradeClassId}")
	@RequiresPermissions("system:dlTradeClass:edit")
	String edit(@PathVariable("tradeClassId") Integer tradeClassId,Model model){
		DlTradeClassDO dlTradeClass = dlTradeClassService.get(tradeClassId);
		model.addAttribute("dlTradeClass", dlTradeClass);
	    return "system/dlTradeClass/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dlTradeClass:add")
	public R save( DlTradeClassDO dlTradeClass){
		if(dlTradeClassService.save(dlTradeClass)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dlTradeClass:edit")
	public R update( DlTradeClassDO dlTradeClass){
		dlTradeClassService.update(dlTradeClass);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dlTradeClass:remove")
	public R remove( Integer tradeClassId){
		try{
			if(dlTradeClassService.remove(tradeClassId)>0){
				return R.ok();
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.error("删除失败，请先删除其下面的行业");
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dlTradeClass:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] tradeClassIds){
		dlTradeClassService.batchRemove(tradeClassIds);
		return R.ok();
	}
	
}
